
const baseRequest = "https://zgz.test.daailianhe.cn/api/v1/"; // 测试域名地址
/**
 * @function 发起网络请求
 * @description 前后端的接口对接
 * @param {String} $path - 服务器接口地址 必填 
 * @param {String} $method - 请求的类型 必填
 * @param {Object} $param - 请求的参数 必填
 * @param {String} $type - 参数内容类型(默认无) 非必填 
 */
const request = (path, method, param, types) => {
	let header
	if (!types) {
		header = {
			Accept: 'application/json',
			// Authorization: '',
			deviceid: uni.getStorageSync('deviceId') ? uni.getStorageSync('deviceId') : '',
			// testUser:1, //仅供测试服
		};
	} else {
		header = {
			Accept: 'application/json',
			// Authorization: 'Bearer ' + uni.getStorageSync('token'),
			deviceid: uni.getStorageSync('deviceId') ? uni.getStorageSync('deviceId') : '',
			// testUser:1, //仅供测试服
		};
	}
	uni.showLoading({
		title:'加载中...',
		mask:true,
	})
	return new Promise((resolve, reject) => {
		uni.request({
			url: baseRequest + path,
			data: param ?? {},
			method: method ?? 'GET',
			header,
			success: res => {
				uni.hideLoading()
				if (res.statusCode == '200') {
					resolve(res)
				}else (res.statusCode=='422'){
					uni.showToast({
						title:res.data.message,
						icon:'none'
					})
				} 
			},
			fail: err => {
				uni.hideLoading()
				// alert(JSON.stringify(err))
				reject(err)
			}
		})
	})
}



//upFiles 上传文件函数
const upFiles =   (url = '', file={}, date={}, fileType,header = {}) => {
	uni.showLoading({
		title:'加载中...',
		mask:true,
	})
	return new Promise((resolve, reject) => {
		//console.log(fileType)
		uni.uploadFile({
			url: baseRequest + url,
			files: file,
			fileType: 'image' || fileType,
			formData: date,
			header: {
				'Accept': 'application/json',
				deviceid: uni.getStorageSync('deviceId') ? uni.getStorageSync('deviceId') : '',
				// testUser:1, //仅供测试服
			} || headedr
		}).then((response) => {
		uni.hideLoading()
			let [error, res] = response;
			res.data = JSON.parse(res.data);
			//console.log(res, 'response');
			resolve(res);
		}).catch(error => {
			let [err, res] = error;
			// //console.log(err, 'error');
			uni.hideLoading()
			reject(err)
		});
	});
}



export default {

	request, //发起网络请求
	upFiles, //文件上传

}
